﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
using System.Data.Linq.SqlClient;
using System.Data.Linq;
using System.Collections;
using System.Data.Objects;
using Library;
using System.Data.Entity.Validation;
using System.Diagnostics;



namespace DAO
{
    /// <summary>
    /// Lien ket voi csdl cua kh
    /// </summary>
    public class CustomerDAO
    {
        BMS_DatabaseEntities context = new BMS_DatabaseEntities();
        /// <summary>
        /// Lay tat ca danh sach khach hang
        /// </summary>
        public List<CustomerEntity> LoadCustomer()
        {

            var query = from customer in context.COMPANY_INFO
                        select new CustomerEntity
                        {
                            CompanyName = customer.COMPANY_NAME,
                            Domain = customer.DOMAIN,
                            Country = customer.COUNTRY,
                            Province = customer.PROVINCE,
                            District = customer.DISTRICT,
                            Address = customer.ADDRESS,
                            Tel = customer.TEL,
                            Mail = customer.EMAIL,
                            Web = customer.WEB,
                            Fax = customer.FAX,
                            StaffName = customer.STAFF_NAME,
                            CustomerCode = customer.COMPANY_CODE
                        };
            return query.ToList<CustomerEntity>();

        }

        /// <summary>
        /// Lay tat ca danh sach khach hang theo sale name
        /// </summary>
        public List<CustomerEntity> LoadCustomerBySale(string Salename)
        {

            var query = from customer in context.COMPANY_INFO
                        where customer.STAFF_NAME == Salename
                        select new CustomerEntity
                        {
                            CompanyName = customer.COMPANY_NAME,
                            Domain = customer.DOMAIN,
                            Country = customer.COUNTRY,
                            Province = customer.PROVINCE,
                            District = customer.DISTRICT,
                            Address = customer.ADDRESS,
                            Tel = customer.TEL,
                            Mail = customer.EMAIL,
                            Web = customer.WEB,
                            Fax = customer.FAX,
                            StaffName = customer.STAFF_NAME,
                            CustomerCode = customer.COMPANY_CODE,
                            ManagedFrom =(DateTime)customer.MANAGED_FROM,
                            ManagedTo=(DateTime)customer.MANAGED_TO,
                        };
            return query.ToList<CustomerEntity>();

        }

        /// <summary>
        /// Tim kiem khach hang
        /// </summary>
        public List<CustomerEntity> FindCustomer(string sFind)
        {
            List<CustomerEntity> result = LoadCustomer().FindAll(
            delegate(CustomerEntity customer)
            {
                if (ConvertString.ConvertToUnSign(customer.StaffName.ToLower()).Contains(sFind) || ConvertString.ConvertToUnSign(customer.CompanyName.ToLower()).Contains(sFind) || ConvertString.ConvertToUnSign(customer.Province.ToLower()).Contains(sFind) || ConvertString.ConvertToUnSign(customer.Country.ToLower()).Contains(sFind) || ConvertString.ConvertToUnSign(customer.Domain.ToLower()).Contains(sFind) || ConvertString.ConvertToUnSign(customer.CustomerCode.ToLower()).Contains(sFind) || ConvertString.ConvertToUnSign(customer.District.ToLower()).Contains(sFind))
                    return true;
                else
                    return false;
            }
        );

            return result;
        }

        /// <summary>
        /// Tim kiem khach hang theo dieu kien
        /// </summary>
        public List<CustomerEntity> FindCustomerBy(string sFind, string sBy)
        {
            List<CustomerEntity> result = LoadCustomer().FindAll(
            delegate(CustomerEntity customer)
            {
                if (sBy == "Company name")
                {
                    if (ConvertString.ConvertToUnSign(customer.CompanyName.ToLower()).Contains(sFind))
                        return true;
                }
                if (sBy == "Domain")
                {
                    if (ConvertString.ConvertToUnSign(customer.Domain.ToLower()).Contains(sFind))
                        return true;
                }
                if (sBy == "Country")
                {
                    if (ConvertString.ConvertToUnSign(customer.Country.ToLower()).Contains(sFind))
                        return true;
                }
                if (sBy == "Province")
                {
                    if (ConvertString.ConvertToUnSign(customer.Province.ToLower()).Contains(sFind))
                        return true;
                }
                if (sBy == "District")
                {
                    if (ConvertString.ConvertToUnSign(customer.District.ToLower()).Contains(sFind))
                        return true;
                }
                if (sBy == "Staff Name")
                {
                    if (ConvertString.ConvertToUnSign(customer.StaffName.ToLower()).Contains(sFind))
                        return true;
                }

                return false;
            }
        );

            return result;

        }


        /// <summary>
        /// Them khach hang
        /// </summary>
        /// <param name="CS">CustomerEntity</param>
        public void AddCustomer(CustomerEntity cs)
        {
            try
            {
                var query = new COMPANY_INFO
                {
                    STAFF_NAME = cs.StaffName,
                    COMPANY_NAME = cs.CompanyName,
                    CONTACT_PERSON = cs.ContactPerson,
                    DOMAIN = cs.Domain,
                    COMPANY_CODE = cs.CustomerCode,
                    TAX_CODE = cs.TaxCode,
                    COUNTRY = cs.Country,
                    PROVINCE = cs.Province,
                    DISTRICT = cs.District,
                    ADDRESS = cs.Address,
                    EMAIL = cs.Mail,
                    WEB = cs.Web,
                    TEL = cs.Tel,
                    FAX = cs.Fax,
                    REMARK = cs.Remark,
                    MANAGED_FROM = cs.ManagedFrom,
                    MANAGED_TO = cs.ManagedTo,
                };
                context.COMPANY_INFO.Add(query);
                context.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
        }

        /// <summary>
        /// Xoa khac hang dua theo ma khach hang
        /// </summary>
        /// <param name="sCustomerCode">ma khach hang duoc truyen vao duoi dang string</param>
        public void DeleteCustomer(string sCustomerCode)
        {
            var query = (from customer in context.COMPANY_INFO
                         where customer.COMPANY_CODE == sCustomerCode
                         select customer).First();
            context.COMPANY_INFO.Remove(query);
            context.SaveChanges();
        }

        /// <summary>
        /// Edit khach hang
        /// </summary>
        /// <param name="CS">CustomerEntity</param>
        public void EditCustomer(CustomerEntity cs)
        {
            var query = (from u in context.COMPANY_INFO where u.COMPANY_CODE == cs.CustomerCode select u).First();

            query.STAFF_NAME = cs.StaffName;
            query.COMPANY_NAME = cs.CompanyName;
            query.CONTACT_PERSON = cs.ContactPerson;
            query.DOMAIN = cs.Domain;
            query.COMPANY_CODE = cs.CustomerCode;
            query.TAX_CODE = cs.TaxCode;
            query.COUNTRY = cs.Country;
            query.PROVINCE = cs.Province;
            query.DISTRICT = cs.District;
            query.ADDRESS = cs.Address;
            query.EMAIL = cs.Mail;
            query.WEB = cs.Web;
            query.TEL = cs.Tel;
            query.FAX = cs.Fax;
            query.REMARK = cs.Remark;

            context.SaveChanges();
        }

        /// <summary>
        /// Edit authorize
        /// </summary>
        /// <param name="CS">CustomerEntity</param>
        public void EditAuthorize(string CustomerCode, string FromSale, string ToSale, DateTime FromDate, DateTime ToDate)
        {
            var query = (from u in context.COMPANY_INFO where u.COMPANY_CODE == CustomerCode & u.STAFF_NAME == FromSale select u).First();
            query.STAFF_NAME = ToSale;
            query.MANAGED_FROM = FromDate;
            query.MANAGED_TO = ToDate;

            context.SaveChanges();
        }

        /// <summary>
        /// Edit khach hang
        /// </summary>
        /// <param name="CS">CustomerEntity</param>
        public CustomerEntity getInfoCustomer(string sCompanyCode)
        {
            var query = (from customer in context.COMPANY_INFO
                         where customer.COMPANY_CODE == sCompanyCode
                         select customer).First();
            CustomerEntity csInfo = new CustomerEntity();
            csInfo.Address = query.ADDRESS;
            csInfo.ContactPerson = query.CONTACT_PERSON;
            csInfo.Province = query.PROVINCE;
            csInfo.CompanyName = query.COMPANY_NAME;
            csInfo.Country = query.COUNTRY;
            csInfo.CustomerCode = query.COMPANY_CODE;
            csInfo.Domain = query.DOMAIN;
            csInfo.Fax = query.FAX;
            csInfo.Mail = query.EMAIL;
            csInfo.Remark = query.REMARK;
            csInfo.StaffName = query.STAFF_NAME;
            csInfo.TaxCode = query.TAX_CODE;
            csInfo.Tel = query.TEL;
            csInfo.Web = query.WEB;
            csInfo.District = query.DISTRICT;

            return csInfo;
        }

        /// <summary>
        /// Check customer code
        /// </summary>
        /// <param name="CS">customer code</param>
        public bool CheckCode(string code)
        {
            var query = (from c in context.COMPANY_INFO where c.COMPANY_CODE == code select c);
            if (query.Count()==0)
            {
                return true;
            }
            return false;
        }
    }

}
